home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 002 / xrf / xrf1.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  6KB  |  199 lines

  1. /*
  2.  *                      ***************
  3.  *                      * X R F 1 . C *
  4.  *                      ***************
  5.  *
  6.  * Lexical processing for C xref'er. Sifts through C source code
  7.  * and pulls out the identifiers. Recognizes reserved words and
  8.  * disregards them. Returns non-zero integer (true) if an id was
  9.  * successfully moved into 'idbuf', zero (false) if end-of-line
  10.  * was reached. I took care to test the C compiler's reaction to
  11.  * Formfeeds with respect to line numbers, and made sure the line
  12.  * numbers assigned by xrf act the same.
  13.  *
  14.  * Version V1.3          9-May-80
  15.  * Version V1.4        10-Jul-80 MM    Allow $ in identifiers, bummed code
  16.  * Version V1.5        21-Jul-80 MM    Dropped newline from ctime()
  17.  * Version V1.6        22-Jul-80 MM    '.' is not an alpha char.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include "xrf.h"
  22.  
  23. #define A 1             /* Alpha character */
  24. #define C 2             /* Start of comment possibly */
  25. #define L 3             /* Literal delimiter */
  26. #define N 4             /* Numeric character */
  27. #define Z 5             /* End of string */
  28.  
  29. #define NRW 28          /* # reserved words */
  30.  
  31. static int cmtflg = 0;    /* Comment flag */
  32.  
  33. static char ctype[] = {         /* Character action lookup */
  34.                         Z,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  35.                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36.                         0,0,L,0,A,0,0,L,0,0,0,0,0,0,0,C,
  37.                         N,N,N,N,N,N,N,N,N,N,0,0,0,0,0,0,
  38.                         0,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,
  39.                         A,A,A,A,A,A,A,A,A,A,A,0,0,0,0,A,
  40.                         0,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,
  41.                         A,A,A,A,A,A,A,A,A,A,A,0,0,0,0,0
  42.                       };
  43.  
  44. static char *reswrd[NRW] = {
  45.                         "auto", "break", "case", "char",
  46.                         "continue", "default", "do", "double",
  47.                         "else", "entry", "extern", "float",
  48.                         "for", "goto", "if", "int",
  49.                         "long", "register", "return", "short",
  50.                         "sizeof", "static", "struct", "switch",
  51.                         "typedef", "union", "unsigned", "while"
  52.                         };
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * Scan off a non reserved identifier. Put it into 'idbuf'.
  78.  * Returns +1 if successful, 0 if end-of-line was reached.
  79.  *
  80.  */
  81.  
  82. getid()                         /* Get an identifier into idbuf */
  83.  
  84.    {
  85.    register char *p ;                   /* Fast scan pointer */
  86.    register char *i ;                   /* Fast id buf pointer */
  87.    register int c;                      /* Dispatch code */
  88.  
  89.    if (debug) {printf("xrf: getting identifier\n");}
  90.    p = scanp;                           /* Init fast pointers */
  91.    i = idbuf;
  92.    while ((c = *p) != 0)                /* Scan till end of string */
  93.       {
  94.       if(c == '\014')                   /* If formfeed, */
  95.          {
  96.          c = *p = ' ';                  /* Convert to harmless blank */
  97.          newpage();                     /* Force new page */
  98.          }
  99.       if (cmtflg)                       /* If comment flag is on */
  100.          {
  101.          while(*p && *p++ != '*');    /* Scan to '*' or end of string */
  102.          if(*p == '/')                  /* If we found end of comment */
  103.             cmtflg = 0;                 /* Turn off comment flag */
  104.          continue;                      /* and recycle */
  105.          }
  106.       switch(ctype[c])            /* Dispatch on the type */
  107.          {
  108.          case A:                        /* Start of an identifier */
  109.             i = idbuf;                  /* Reset the id buffer pointer */
  110.             do {
  111.                if( i < &idbuf[NCPS] ) { /* Copy into idbuf */
  112.                   *i++ = *p++;
  113.                } else {                  /* Unless it gets full */
  114.                   p++;
  115.         }
  116.             } while( (c=ctype[*p]) == A || c == N ); /* While alphanumeric */
  117.             while( i < &idbuf[NCPS] ) {  /* Pad idbuf with nulls */
  118.                *i++ = '\0';
  119.         }
  120.             if(nonres(idbuf))           /* If it's not a reserved word */
  121.                {
  122.                scanp = p;               /* Update the scan pointer */
  123.         if (debug) {printf("xrf: got identifier %s\n",idbuf);}
  124.                return (1);              /* Return with success */
  125.                }
  126.             break;                      /* End of identifier processing */
  127.          case N:                        /* Scan by a number string */
  128.             do p++; while( (c=ctype[*p]) == N || c == A );
  129.             break;
  130.          case L:                        /* Scan by a literal */
  131.         while (*++p != c && *p) {    /* Scan to the matching trailing */
  132.         if (*p == '\\') p++;    /* Quote, ignoring backslash quoted */
  133.         }                /* Characters.  If not at the end */
  134.         if (*p) p++;        /* Of the line, skip to the char. */
  135.             break;            /* Following the trailing quote */
  136.          case C:
  137.             if(*++p == '*')             /* Start a comment */
  138.               cmtflg = 1;               /* by setting comment flag */
  139.             break;
  140.          default:                       /* Otherwise just scan it off */
  141.             p++;
  142.             break;
  143.          }                              /* End of switch statement */
  144.       }                         /* If we exit here, end-of line. */
  145.    if (debug) {printf("xrf: get identifier failed\n");}
  146.    return(0);                   /* Return with failure indication */
  147.    }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /*
  168.  * Search for reserved word. Return true (1) if NOT reserved word.
  169.  * Uses binary search.
  170.  */
  171.  
  172. nonres( bufp )                  /* Test if not reserved word */
  173. char *bufp;
  174. {
  175.    register int low ;           /* Low pointer index */
  176.    register int mid ;           /* Mid ... */
  177.    register int hi  ;           /* hi ...  */
  178.    int cond;                    /* Condition from strcmp */
  179.  
  180.    if (debug) {printf("xrf: check %s for reserved\n",bufp);}
  181.    low = 0;
  182.    hi  = NRW-1;
  183.    while (low <= hi) {
  184.        mid = (low + hi) / 2;
  185.        if((cond = strcmp(bufp, reswrd[mid])) < 0) {
  186.            hi = mid - 1;
  187.        } else if (cond > 0) {
  188.            low = mid + 1;
  189.        } else {
  190.        if (debug) {printf("xrf: %s is reserved\n",bufp);}
  191.            return(0);             /* False, it IS reserved */
  192.        }
  193.    }
  194.    if (debug) {printf("xrf: %s not reserved\n",bufp);}
  195.    return(1);                   /* True, it's NOT reserved */
  196. }
  197.  
  198.  
  199.